<?php

namespace App\Http\Controllers\Admin;

use App\CodeResponse;
use App\Http\Controllers\Controller;
use App\Models\ClassStudent;
use App\Models\StudentWork;
use App\Models\Users;
use App\Services\ClassGradeService;
use App\Services\ClassStudentService;
use App\Services\SchoolService;
use App\Services\UsersServices;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class ClassStudentController extends AdminController
{
    //添加班级学生
    public function  add(Request $request){

        $validator = Validator::make($request->all(), [
            'username' => 'required',
            'password' => 'required',
            'name' => 'required',
            'school_id' => 'required|integer|digits_between:1,20|min:1',
            'class_grades_id' => 'integer|digits_between:1,20|min:1',
            'sex' => 'integer|digits_between:1,20',

            'age' => 'integer|digits_between:1,20',
            'email' => 'required',
            'phone' => 'required',
            'pic' => 'image|size:512',
            'student_num' => 'required|digits_between:1,20|min:1',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }


        $school =  SchoolService::getInstance()->getSchoolInfo($request->school_id);
        $studentNum =  SchoolService::getInstance()->getSchoolStudentNum($request->school_id);
        if(!is_null($school) && $studentNum  >= $school->student_num ){
            return $this->fail(CodeResponse::AUTH_SCHOOL_STUDENT_NUM);
        }

        $validator = Validator::make($request->all(), [
            'phone' => 'regex:/^1[345789][0-9]{9}$/',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::AUTH_PHONE_UNREGISTERED);
        }

        $validator = Validator::make($request->all(), [
            'email' => 'email:rfc,dns',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::AUTH_MEAIL_UNREGISTERED);
        }

        $user =  UsersServices::getInstance()->getByUsresname($request->username);
        if(!is_null($user)){
            return $this->fail(CodeResponse::AUTH_NAME_REGISTERED);
        }
        $user =  UsersServices::getInstance()->getByPhone($request->phone);
        if(!is_null($user)){
            return $this->fail(CodeResponse::AUTH_PHONE_REGISTERED);
        }
        $user =  UsersServices::getInstance()->getByEmail($request->email);
        if(!is_null($user)){
            return $this->fail(CodeResponse::AUTH_MEAIL_REGISTERED);
        }

        $users = new Users();
        $users->username = $request->username;
        $users->password = Hash::make($request->password);
        $users->name = $request->name;
        $users->sex = $request->sex;
        $users->age = $request->age;
        $users->pic = $request->pic;
        $users->type = 0;
        $users->email = $request->email;
        $users->phone = $request->phone;
        $users->school_id = $request->school_id;
        $users->student_num = $request->student_num;


        $users->last_login_time =Carbon::now()->toDateTimeString();
        $users->last_login_ip = $request->getClientIp();
        $users->save();

        $classStudent = new ClassStudent();
        $classStudent->class_grades_id = $request->class_grades_id;
        $classStudent->school_id = $request->school_id;
        $classStudent->users_id = $users->id;
        $classStudent->save();

        return $this->success();
    }

    public function update(Request $request)
    {

        $validator = Validator::make($request->all(), [
            'users_id' => 'required|integer|digits_between:1,20',
            'school_id' => 'required|integer|digits_between:1,20',
            'class_grades_id' => 'required|integer|digits_between:1,20',
            'type' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $validator = Validator::make($request->all(), [
            'username' => 'unique:users,username,' . $request->users_id . ',id',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::AUTH_NAME_REGISTERED);
        }
        $validator = Validator::make($request->all(), [
            'email' => 'unique:users,email,' . $request->users_id . ',id',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::AUTH_MEAIL_REGISTERED);
        }
        $validator = Validator::make($request->all(), [
            'phone' => 'unique:users,phone,' . $request->users_id . ',id',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::AUTH_PHONE_REGISTERED);
        }

        $putData = $request->except(['users_id','class_grades_id']);
        $putData['password'] =  Hash::make($request->password);
        //更新用户表
        $users = new Users();

        $users->find($request->users_id)->update($putData);

        //更新班级学生关联表
        $classStudent = new ClassStudent();
        $classStudent->where('users_id',$request->users_id)
            ->update([
                'users_id' =>$request->users_id,
                'school_id' => $request->school_id,
                'class_grades_id' => $request->class_grades_id,
            ]);
        return $this->success();

    }
    public function list(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'page' => 'integer',
            'limit' => 'integer',
            'username' => 'string',
            'school_id' => 'integer|digits_between:1,20|min:1',
            'class_grades_id' => 'integer|digits_between:1,20|min:1',
            'sort' => 'string',
            'order' => 'string',

        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $username = $request->input('username');
        $school_id = $request->input('school_id');
        $class_grades_id = $request->input('class_grades_id');

        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $sort = $request->input('sort','created_at');
        $order = $request->input('order','desc');

        $list = ClassStudentService::getInstance()->listClassStudent($username,$school_id,$class_grades_id,$page,$limit,$sort,$order);

        return $this->success($list);

    }
    //获取学校人数，班级人数等
    public function getMost(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'school_id' => 'required|integer|digits_between:1,20|min:1',
            'class_grades_id' => 'required|integer|digits_between:1,20|min:1',

        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $school_id = $request->input('school_id');
        $class_grades_id = $request->input('class_grades_id');
        $school =  SchoolService::getInstance()->getSchoolInfo($school_id);
        $studentNum =  SchoolService::getInstance()->getSchoolStudentNum($school_id);
        $classGradeStudentNum =  ClassGradeService::getInstance()->getClassGradeStudentNum($school_id,$class_grades_id);
        $schoolNum = 0;
        if($school){
            $schoolNum= $school->student_num;
        }

        $arr=[
            'schoolNum' =>$schoolNum,//学校可添加人数
            'studentNum' => $studentNum,//学校已经添加人数
            'classStudentNum' => $classGradeStudentNum //班级已经添加人数
            ];
        return $this->success($arr);
    }
    //删除
    public function delete(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'users_id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $users = Users::find($request->users_id);
        $users->delete();
        $classStudent = ClassStudent::where('users_id',$request->users_id);
        $classStudent->delete();
        return $this->success();
    }


}
